//
//  WQHomeViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/17.
//

#import "WQHomeViewController.h"
#import "WQHomeWeatherHeaderView.h"
#import "WQHomeDeviceListCell.h"
#import "WQHomeOneSixSixCell.h"
#import "WQHomeEditFootView.h"

typedef NS_ENUM(NSUInteger, HomeCellType) {
    HomeCellTypeDeviceList,
    HomeCellType166,
};

@interface WQHomeViewController () <CLLocationManagerDelegate>

@property (nonatomic, strong) UIImageView           *backColorImageView;

@property (nonatomic, strong) UIButton           *logoButton;
@property (nonatomic, strong) UIButton           *framilyButton;

@property (nonatomic, strong) UIButton           *rightMicrButton; // 麦克风
@property (nonatomic, strong) UIButton           *rightAddButton; // +

@property (nonatomic, strong) WQHomeWeatherHeaderView           *homeWeaterHeadView;
@property (nonatomic, strong) WQHomeEditFootView           *homeEditFootView;

@property(strong, nonatomic) CLLocationManager *locationManager;
@property(assign, nonatomic) double longitude;
@property(assign, nonatomic) double latitude;

@end

@implementation WQHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.hbd_barTintColor = UIColor.clearColor;
    self.hbd_barStyle = UIBarStyleBlack;
    self.hbd_barAlpha = 0;
    
    self.navigationItem.leftBarButtonItems = @[
        [[UIBarButtonItem alloc] initWithCustomView:self.logoButton],
        [[UIBarButtonItem alloc] initWithCustomView:self.framilyButton]
    ];
    self.navigationItem.rightBarButtonItems = @[
        [[UIBarButtonItem alloc] initWithCustomView:self.rightAddButton],
//        [[UIBarButtonItem alloc] initWithCustomView:self.rightMicrButton]
    ];
    
    kWEAKSELF
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf requestHandlerWishIsRefresh:YES];
    }];
    
    header.lastUpdatedTimeLabel.textColor = UIColor.whiteColor;
    header.stateLabel.textColor = UIColor.whiteColor;
    self.tableView.mj_header = header;
    
    self.tableView.tableHeaderView = self.homeWeaterHeadView;
    self.tableView.tableFooterView = self.homeEditFootView;
    [self.view addSubviews:[NSArray arrayWithObjects:self.backColorImageView, nil]];
    [self.view insertSubview:self.backColorImageView atIndex:0];
    [self fragmentUI_ViewSet];
    
    [WQShare loginSuccessMethods];
    
    [self.locationManager requestWhenInUseAuthorization];
    
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager startUpdatingHeading];
    } else {
        DEBUGLOG(@"定位失败1");
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadHomeWeaterHeadViewMethods) name:kNotificationFamilyUpdate object:nil];
    
    
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *location = manager.location;
    if (!location) {
        return;
    }
    
    self.longitude = location.coordinate.longitude;
    self.latitude = location.coordinate.latitude;
}

#pragma mark ------- Http Mehods -------

- (void)requestHandlerWishIsRefresh:(BOOL)isRefresh {
    [self tableEndRefresh];
}

#pragma mark ------- Private Methods -------

// 更新家庭，天气
- (void)reloadHomeWeaterHeadViewMethods {
    
    if (WQShare.isLogin) {
        self.homeWeaterHeadView.roomTitleLabel.text = WQShare.userHomeModel.homeModel.name;
        
        
    } else {
        self.homeWeaterHeadView.roomTitleLabel.text = @"请先登陆";
    }
}

#pragma mark ------- UITableViewDelegate -------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case HomeCellTypeDeviceList:
            return kFragmemtFloat(150) * 1;
            
        case HomeCellType166:
        {
            return kFragmemtFloat(65);
        }
            
        default:
            return kFragmemtFloat(65);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case HomeCellTypeDeviceList:
        {
            WQHomeDeviceListCell *cell = [WQHomeDeviceListCell ff_CellDequeueWithId:tableView];
            return cell;
        }
            
        case HomeCellType166:
        {
            WQHomeOneSixSixCell *cell = [WQHomeOneSixSixCell ff_CellDequeueWithId:tableView];
            return cell;
        }
            
        default:
            return [UITableViewCell ff_CellDequeueWithId:tableView];
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.backColorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (UIImageView *)backColorImageView {
    if (!_backColorImageView) {
        _backColorImageView = [[UIImageView alloc] initWithImage:kImageName(@"base_index_big_bg")];
        _backColorImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _backColorImageView;
}

- (WQHomeWeatherHeaderView *)homeWeaterHeadView {
    if (!_homeWeaterHeadView) {
        _homeWeaterHeadView = [[WQHomeWeatherHeaderView alloc] init];
        _homeWeaterHeadView.frame = CGRectMake(0, 0, kScreen_Width, kFragmemtFloat(350));
        
        kWEAKSELF
        // 所有设备
        [_homeWeaterHeadView.allDidImageView kj_AddTapGestureRecognizerBlock:^(UIView * _Nonnull view, UIGestureRecognizer * _Nonnull gesture) {
            
            if (WQShare.isLoginUserAndPresent) {
                WQUserAllDeviceViewController *vc = [[WQUserAllDeviceViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }];
        
        // 房间列表
        [_homeWeaterHeadView.roomDidImageView kj_AddTapGestureRecognizerBlock:^(UIView * _Nonnull view, UIGestureRecognizer * _Nonnull gesture) {
            
            if (WQShare.isLoginUserAndPresent) {
                WQUserAllRoomViewController *vc = [[WQUserAllRoomViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
    return _homeWeaterHeadView;
}

- (UIButton *)logoButton {
    if (!_logoButton) {
        _logoButton = [UIButton initWithImage:kImageName(@"base_nav_logo") selectedImage:nil];
    }
    return _logoButton;
}

- (UIButton *)framilyButton {
    if (!_framilyButton) {
        _framilyButton = [UIButton initWithImage:kImageName(@"base_nav_framilywhite") selectedImage:nil];
//        [_framilyButton setTitle:@"  是大啊了空间斯啊打死" forState:0];
        [_framilyButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
            [WQShare showExchangeFramilyListView];
        }];
    }
    return _framilyButton;
}

- (UIButton *)rightMicrButton {
    if (!_rightMicrButton) {
        _rightMicrButton = [UIButton initWithImage:kImageName(@"home_nav_voice") selectedImage:nil];
    }
    return _rightMicrButton;
}

- (UIButton *)rightAddButton {
    if (!_rightAddButton) {
        _rightAddButton = [UIButton initWithImage:kImageName(@"home_nav_add") selectedImage:nil];
    }
    return _rightAddButton;
}

- (WQHomeEditFootView *)homeEditFootView {
    if (!_homeEditFootView) {
        _homeEditFootView = [[WQHomeEditFootView alloc] init];
        _homeEditFootView.frame = CGRectMake(0, 0, kScreen_Width, kFragmemtFloat(60));
        
        kWEAKSELF
        [_homeEditFootView.editButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
            if (WQShare.isLoginUserAndPresent) {
                OCBaseViewController *vc = [[OCBaseViewController alloc] init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }
        }];
    }
    return _homeEditFootView;
}

- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
    }
    return _locationManager;
}


@end
